package fr.insta.reverse.uml.dao;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import fr.insta.reverse.uml.bean.Visibilite;
import fr.insta.reverse.uml.util.HibernateUtil;

/**
 * DAO for domain model class Visibilite.
 * @see fr.insta.reverse.uml.bean.Visibilite
 * @author Valera
 */
@Stateless
public class DaoVisibilite implements IDao<Visibilite>
{
	private static final Log log = LogFactory.getLog(DaoVisibilite.class);

	@Override
	public Visibilite save(Visibilite vis)
	{
		log.debug("saving Visibilite instance");
		try
		{
			Session session = HibernateUtil.currentSession();
			Transaction tx = session.beginTransaction();

			session.saveOrUpdate(vis);

			tx.commit();
			HibernateUtil.closeSession();
			log.debug("save successful");
			return vis;
		}
		catch (RuntimeException e)
		{
			log.error("save failed", e);
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public boolean remove(Visibilite vis)
	{
		log.debug("removing Visibilite instance");
		try
		{
			Session session = HibernateUtil.currentSession();
			Transaction tx = session.beginTransaction();

			vis = (Visibilite) session.createCriteria(Visibilite.class)
					.add(Restrictions.eq("lib", vis.getLib()))
					.uniqueResult();

			session.delete(vis);

			tx.commit();
			HibernateUtil.closeSession();
			log.debug("remove successful");
			return true;
		}
		catch (RuntimeException e)
		{
			log.error("remove failed", e);
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public Visibilite findById(int id)
	{
		log.debug("getting Visibilite instance with id: " + id);
		try
		{
			Session session = HibernateUtil.currentSession();

			Visibilite instance = (Visibilite) session.createCriteria(Visibilite.class)
					.add(Restrictions.eq("id", id))
					.uniqueResult();

			HibernateUtil.closeSession();
			log.debug("get successful");
			return instance;
		}
		catch (RuntimeException re)
		{
			log.error("get failed", re);
			re.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Visibilite> getAll()
	{
		log.debug("getting all Visibilites");
		List<Visibilite> listusers = new ArrayList<Visibilite>();
		try
		{
			Session session = HibernateUtil.currentSession();

			listusers = (List<Visibilite>) session.createCriteria(Visibilite.class).list();

			HibernateUtil.closeSession();
			log.debug("get successful");
			return listusers;
		}
		catch (RuntimeException re)
		{
			log.error("get failed", re);
			re.printStackTrace();
		}
		return null;
	}

	@Override
	public boolean deleteAll()
	{
		log.debug("deleting all Visibilites");
		try
		{
			Session session = HibernateUtil.currentSession();
			Transaction tx = session.beginTransaction();

			session.createQuery("delete from Visibilite").executeUpdate();

			tx.commit();
			HibernateUtil.closeSession();
			log.debug("delete successful");
			return true;
		}
		catch (RuntimeException re)
		{
			log.error("delete failed", re);
			re.printStackTrace();
		}
		return false;
	}
}
